package com.teste.websocket;

import java.io.IOException;
import java.util.Set;

import javax.websocket.CloseReason;
import javax.websocket.EndpointConfig;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

@ServerEndpoint("/teste")
public class TesteWebSocket {
	@OnMessage
	public void onMessage(Session session, String msg) {
		try {
			Set<Session> openSessions = session.getOpenSessions();
			for (Session s : openSessions) {
				s.getBasicRemote().sendText(msg);
			}
			//session.getBasicRemote().sendText(msg);
			System.out.println(msg);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@OnOpen
	public void open(Session session, EndpointConfig conf) {
		System.out.println("opa");
	}

	@OnError
	public void error(Session session, Throwable error) {

	}

	@OnClose
	public void close(Session session, CloseReason reason) {

	}
}
